package sk.tuke.android4all.dialer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.View.OnGenericMotionListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import sk.tuke.android4all.controls.CustomFragment;
import sk.tuke.android4all.listeners.SwipeListener;
import sk.tuke.android4all.services.Tts;
import android.view.GestureDetector;

public class NewContactFragment extends CustomFragment {

	private static String TAG = "NewContactFragment";
	private Activity mActivity = null;
	private ContactInfo mContact = null;
	int touchCounter = 0;
	private final int TOUCH_INTENSITY = 4;
	private SwipeListener mActionListener = null;
	private Tts mSpeechEngine;
	private int selectedTag = -1;
	
	public NewContactFragment(Activity aActivity_in, ContactInfo aContact_in){
		mActivity = aActivity_in;
		mContact = aContact_in;
		mSpeechEngine = new Tts(mActivity);
	}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_newcontact, container, false);	       
        return rootView;
    }
	
	@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        registerTouchListener();
        if(mContact !=null){
        	createEditUI();
        }else{
        	createCreateUI();
        }
    }
	
	private void registerTouchListener() {
		mActionListener =  new SwipeListener(mActivity) {
	        @Override
	        public void onDoubleTapAction(int count) {
	            Log.i("TAG", "DoubleTapped " + selectedTag);
	            resetTouchCounter();
            	if(selectedTag == R.id.fragmentnewcontact_confirm){
            		Log.i(TAG, "OnDoubleTapAction - confirm");
            		mSpeechEngine.sayMessage(getResources().getString(R.string.fragmentnewcontact_confirm));
            		saveChanges();
            	}else if(selectedTag == R.id.fragmentnewcontact_name){
            		Log.i(TAG, "OnDoubleTapAction - name");
            		mSpeechEngine.sayMessage(getResources().getString(R.string.fragmentnewcontact_name));
            		setEditText(selectedTag);
            	}else if(selectedTag == R.id.fragmentnewcontact_number){
            		Log.i(TAG, "OnDoubleTapAction - number");
            		setEditText(selectedTag);
            	}
	        }
		
			@Override
			public void onZoomInAction() {
				// TODO Auto-generated method stub
				
			}
	
			@Override
			public void onZoomOutAction() {
				// TODO Auto-generated method stub
				
			}
		};
		getView().setOnTouchListener(mActionListener);
		
	}
	
	private void setControl(final int controlId, String value) {
		final TextView controlTv = (TextView) getView().findViewById(controlId);
		if(value!=null){
			controlTv.setText(value);
		}
		controlTv.setOnGenericMotionListener(new OnGenericMotionListener(){
	
			@Override
			public boolean onGenericMotion(View arg0, MotionEvent arg1) {
				mActionListener.resetTouchCounter();
				String textInControl = controlTv.getText().toString();
	            if (selectedTag != controlId && touchCounter>TOUCH_INTENSITY) {
	                touchCounter = 0;
	                mSpeechEngine.sayMessage(textInControl);
	                selectedTag = controlId;
	            }else{
	                touchCounter++;
	            }
	            return false;
			}});		
	}
	

	private void createCreateUI() {		
		setControl(R.id.fragmentnewcontact_name, null);
		setControl(R.id.fragmentnewcontact_number, null);
		setControl(R.id.fragmentnewcontact_confirm, null);
	}

	private void createEditUI() {
		setControl(R.id.fragmentnewcontact_name, mContact.getName());
		setControl(R.id.fragmentnewcontact_number, mContact.getNumber());
		setControl(R.id.fragmentnewcontact_confirm, null);
	}
	
	private void saveChanges() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void returnAction() {
		mActivity.getFragmentManager().beginTransaction().replace(R.id.container, new ContactsFragment(mActivity),  ContactsFragment.class.getName()).commit();
	}
	
	private void setEditText(int aControlId) {		
		Intent mTextRequest = new Intent("sk.tuke.android4all.textprovider");
		mTextRequest.putExtra("text", "");
		mTextRequest.putExtra("caller", getClass().getName());
		mActivity.startActivityForResult(mTextRequest, aControlId);
	}
	
	@Override
	public void proceedReturnedResult(int requestCode, int resultCode, Intent in){
		Log.i(TAG, "proceedReturnedResult");
	 	((TextView)getView().findViewById(requestCode)).setText(in.getExtras().getString("text"));
    }
}
